

import { SqlBuilder } from './SqlBuilder'
import { ScriptApi } from "../api/scriptApi"
import { metadataCovert, metaDefaultConfg } from "./constant"
class MetadataService {

  
    constructor(request, message) {
        this.request = request;
        this.message = message;
        this.sqlBuilder = new SqlBuilder();
        this.scriptApi = new ScriptApi(request, message)
    }




    getCatalogs(connId) {
        const rowCovert = (d, row) => {
            d.name = row.SCHEMA_NAME;
            d.id = `${connId}_${d.name}`;
            d.type = 'CATALOG';
            d.properties.catalog = d.name;

        }
        return new Promise((resolve, reject) => {
            const executeParams = {
                script: this.sqlBuilder.selectCatalogs(), noPage: true
            };
            const params = {
                connId: connId,
                executeParams: executeParams
            };
            this.scriptApi.singleExecute(params).then(res => {
                resolve(metadataCovert(this.opt, rowCovert, res, connId))
            }).catch(e => {
                reject(e)
            })
        })
    }

    getTables(connId, catalog) {

        const rowCovert = (d, row) => {

            d.name = row.TABLE_NAME;
            d.id = `${connId}_${catalog}_${d.name}`;
            d.type = 'TABLE';
            d.properties.tableName = d.name;
            d.leaf = true;

        }
        return new Promise((resolve, reject) => {
            const executeParams = {
                script: this.sqlBuilder.selectTables(catalog), noPage: true
            };
            const params = {
                connId: connId,
                executeParams: executeParams
            };
            this.scriptApi.singleExecute(params).then(res => {
                resolve(metadataCovert(this.opt, rowCovert, res, connId, catalog))
            }).catch(e => {
                reject(e)
            })
        })
    }

    getFunctions(connId, catalog) {
        const rowCovert = (d, row) => {
			d.name = row.ROUTINE_NAME;
			d.id = `${connId}_${catalog}_${d.name}`;
			d.type = 'FUNCTION';
			d.properties.functionName = d.name;
			d.leaf = true;
	
		}
        return new Promise((resolve, reject) => {
            const executeParams = {
                script: this.sqlBuilder.selectFunctions(catalog), noPage: true
            };
            const params = {
                connId: connId,
                executeParams: executeParams
            };
            this.scriptApi.singleExecute(params).then(res => {
                resolve(metadataCovert(this.opt, rowCovert, res, connId,catalog,tableName))
            }).catch(e => {
                reject(e)
            })
        })
    }

    getViews(connId, catalog) {
        const rowCovert = (d, row) => {
			d.name = row.ROUTINE_NAME;
			d.id = `${connId}_${catalog}_${d.name}`;
			d.type = 'VIEW';
			d.properties.functionName = d.name;
			d.leaf = true;
	
		}
        return new Promise((resolve, reject) => {
            const executeParams = {
                script: this.sqlBuilder.selectViews(catalog), noPage: true
            };
            const params = {
                connId: connId,
                executeParams: executeParams
            };
            this.scriptApi.singleExecute(params).then(res => {
                resolve(metadataCovert(this.opt, rowCovert, res, connId,catalog,tableName))
            }).catch(e => {
                reject(e)
            })
        })
    }

    getTableColumns(connId, catalog, tableName) {
        return new Promise((resolve, reject) => {
            const executeParams = {
                script: this.sqlBuilder.selectColumns(catalog, tableName), noPage: true
            };
            const params = {
                connId: connId,
                executeParams: executeParams
            };
            this.scriptApi.singleExecute(params).then(res => {
                resolve(res)
            }).catch(e => {
                reject(e)
            })
        })
    }

    getTableIndexs(connId, catalog, tableName) {
        return new Promise( async (resolve, reject) => {
            const executeParams = {
                script: this.sqlBuilder.selectIndexs(catalog, tableName), noPage: true
            };
            const params = {
                connId: connId,
                executeParams: executeParams
            };
            resolve( await  this.scriptApi.singleExecute(params))

        })
    }

    getTableForeignKeys(connId, catalog, tableName) {
        return new Promise((resolve, reject) => {
            const executeParams = {
                script: this.sqlBuilder.selectTableForeignKeys(catalog, tableName), noPage: true
            };
            const params = {
                connId: connId,
                executeParams: executeParams
            };
            this.scriptApi.singleExecute(params).then(res => {
                resolve(res)
            }).catch(e => {
                reject(e)
            })
        })
    }

    getTableTriggers(connId, catalog, tableName) {
        return new Promise((resolve, reject) => {
            const executeParams = {
                script: this.sqlBuilder.selectTableTriggers(catalog, tableName), noPage: true
            };
            const params = {
                connId: connId,
                executeParams: executeParams
            };
            this.scriptApi.singleExecute(params).then(res => {
                resolve(res)
            }).catch(e => {
                reject(e)
            })
        })
    }

    getTableForeignKeys(connId, catalog, tableName) {
        return new Promise((resolve, reject) => {
            const executeParams = {
                script: this.sqlBuilder.selectTableForeignKeys(catalog, tableName), noPage: true
            };
            const params = {
                connId: connId,
                executeParams: executeParams
            };
            this.scriptApi.singleExecute(params).then(res => {
                resolve(res)
            }).catch(e => {
                reject(e)
            })
        })
    }

  

}


export {
    MetadataService
}